<?php
/*
  [EWEISHOP] Copyright(C) 2017 Elinkint Inc.
  This is NOT a freeware, use is subject to license terms
   
  OpenPlatformMiniProgramAccount.php 2018-05-30 12:06 cunx $
   
*/

namespace common\library\wechat\openplatform;

use common\component\wechat\Wechat;
use common\library\wechat\WechatApi;
/**
 * 第三方平台代管理小程序账号设置
 * Class OpenPlatformMiniProgram
 * @package common\library\wechat\openplatform
 * @author cunx
 */
class OpenPlatformMiniProgramAccount extends WechatApi
{
    public static $apiType = 'miniProgram';
    public static $forceOpenPlatform = true;

    /**
     * 设置小程序服务器域名
     * @param string $action            操作名称，add添加, delete删除, set覆盖, get获取
     * @param array $requestDomain      request合法域名，当action参数是get时不需要此字段
     * @param array $wsrequestDomain    socket合法域名，当action参数是get时不需要此字段
     * @param array $uploadDomain       uploadFile合法域名，当action参数是get时不需要此字段
     * @param array $downloadDomain     downloadFile合法域名，当action参数是get时不需要此字段
     * @return array|bool|void
     * @cunx
     */
    public static function modifyDomain(string $action, array $requestDomain = [], array $wsrequestDomain = [], array $uploadDomain = [], $downloadDomain = []){
        $params = [
            'action'            =>  $action,
            'requestdomain'     =>  $requestDomain,
            'wsrequestdomain'   =>  $wsrequestDomain,
            'uploaddomain'      =>  $uploadDomain,
            'downloaddomain'    =>  $downloadDomain
        ];
        try{
            $result = self::getInstance()->domain->modify($params);
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 绑定微信用户为小程序体验者
     * @param string $wechatId  微信账号
     * @return array|bool|void
     */
    public static function testerBind(string $wechatId){
        try{
            $result = self::getInstance()->tester->bind($wechatId);
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 解除绑定小程序的体验者
     * @param string $wechatId  微信账号
     * @return array|bool|void
     */
    public static function testerUnbind(string $wechatId){
        try{
            $result = self::getInstance()->tester->unbind($wechatId);
        }catch (\Exception $result){}

        return Wechat::apiError($result);
    }

    /**
     * 获取体验者列表
     * @return array|bool|void
     */
    public static function testerList(){
        try{
            $result = self::getInstance()->tester->list();
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }


    /**
     * 创建开放平台帐号并绑定小程序
     * @return array|bool|void
     */
    public static function createOpenPlatform(){
        try{
            $result = self::getInstance()->account->create();
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 将小程序绑定到开放平台帐号下
     * @param string $openAppId
     * @return array|bool|void
     */
    public static function bindToOpenPlatform(string $openAppId){
        try{
            $result = self::getInstance()->account->bindTo($openAppId);
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 将小程序从开放平台帐号下解绑
     * @return array|bool|void
     */
    public static function unbindFromOpenPlatform(string $openAppId){
        try{
            $result = self::getInstance()->account->unbindFrom($openAppId);
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }

    /**
     * 获取公众号/小程序所绑定的开放平台帐号
     * @return array|bool|void
     */
    public static function getBinding(){
        try{
            $result = self::getInstance()->account->getBinding();
        }catch (\Exception $result){}
        return Wechat::apiError($result);
    }
}